import json
from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.

# 演示从请求体中读取非表单数据
def get_body_json(request):
    # 获取请求体内容
    json_str = request.body
    # 讲请求体内容由二进制解码成json格式
    json_str = json_str.decode()
    print(json_str)
    # 将请求体内容由json格式转换称python字典格式
    req_data = json.loads(json_str)
    print(req_data)

    print(req_data['a'])
    print(req_data['b'])

    return HttpResponse('OK')

# 演示提取请求体中的表单数据
def get_body_form(request,city, year):
    like = request.POST.get('a')
    like_list = request.POST.getlist('a')
    print(like, like_list[1])
    return HttpResponse('OK')


# 演示提取查询字符串
# GET 和请求类型无关都是用GET获取

def weather3(request, year, city):
    a = request.GET.get('a')
    b = request.GET.get('b')
    c = request.GET.getlist('a')
    print(city, year, a, b, c)
    return HttpResponse('OK')

# 演示正则的组提取url路径参数 关键字参数
def weather2(request, year, city):
    print(city, year)
    return HttpResponse('OK')

# 演示正则的组提取url路径参数 位置参数
def weather1(request, city, year):
# def weather1(request):

    print(city, year)
    return HttpResponse('OK')
